<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * 返回JSON
     * @param mixed(array|Collection) $data 数据
     * @param string $tip 提示
     * @param string $status 状态
     */
    public static function json($data, $tip='', $status = 200) {

        $return = array();
        $return['code']   = $status;
        $return['message']      = $tip;

        //处理数据
        if ($data) {
            if (is_array($data)) {
                foreach ($data as $key => &$item) {
                    if($item instanceof Collection) {
                        $data[$key] = $item->toArray();
                    }
                }
                unset($item);
            } elseif ($data instanceof Collection) {
                $data = $data->toArray();
            }

            self::sanitizeApiData($data);

            $return['data']['cnt'] = $data;

        } else {

            $return['data']['cnt'] = [];

        }

        if (\Request::input('debug') == 1) {
            return view('app.json', ['return'=>json_encode($return)]);
        } else {
            return response()->json($return);
        }


    }

    /**
     * 返回JSON
     * @param mixed(array|Collection) $data 数据
     * @param string $tip 提示
     * @param string $status 状态
     */
    public static function se_json($tip='', $status = 200) {

        $return = array();
        $return['code']   = $status;
        $return['message']      = $tip;

        //处理数据
        $return['data'] = null;

        if (\Request::input('debug') == 1) {
            return view('app.json', ['return'=>json_encode($return)]);
        } else {
            return response()->json($return);
        }


    }

    /**
     * 返回操作成功的JSON，用于不需要返回具体数据的请求。
     * @param unknown $tip
     * @param string $status
     */
    public static function success($tip, $status = 200) {
        return self::se_json($tip, $status);
    }

    /**
     * 返回操作失败的JSON
     * @param mixed $tip
     * @param string $status
     */
    public static function error($tip, $status = 400) {
        if ($tip instanceof \Illuminate\Support\MessageBag) {//$validator->message()返回MessageBag
            $tip = $tip->first();
        }
        return self::se_json($tip, $status);
    }

    /**
     * 清理API返回的数据
     * @param array $arr
     * @return array
     */
    public static function sanitizeApiData(&$arr)
    {
        foreach ($arr as &$value)
        {
            if (is_array($value))
            {
                if ( !empty($value) )
                {
                    self::sanitizeApiData($value);
                }
                else
                {
                    $value = null;
                }

            }
        }

        return $arr;
    }

}
